﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using Newtonsoft.Json;

namespace WebApiForHealthCheck
{
    public class PushAPPMsgManger
    {       
        private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
        private static String APPID = "444b6p6DI59gtn2q6BBh73";
        private static String APPKEY = "J8AzsT6YtsATtt6SXbgPx9";
        private static String MASTERSECRET = "KdHxncHymj5MgrMwVyBZ32";
        private static IIGtPush _client;
        //privat
        private static readonly object Locker = new object();

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <returns></returns>
        public static IIGtPush CreateClient
        {
            get
            {
                if (_client == null)
                {
                    lock (Locker)
                    {
                        if (_client == null)
                        {
                            _client = GetClient();
                        }
                    }
                }
                return _client;
            }
        }

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        private static IGtPush GetClient()
        {
            return new IGtPush(HOST, APPKEY, MASTERSECRET);
        }

        public static string PushMessageToList(IEnumerable<string> clients, MessageBodyExtra messageBody)
        {
            // 推送主类
            var push = CreateClient; 
            ListMessage message = new ListMessage();

            NotificationTemplate template = NotificationTemplateDemo(messageBody);
            // 用户当前不在线时，是否离线存储,可选
            message.IsOffline = true;
            // 离线有效时间，单位为毫秒，可选
            message.OfflineExpireTime = 1000 * 3600 * 12;
            message.Data = template;
            //message.PushNetWorkType = 0;        //判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
            //设置接收者
            List<Target> targetList = new List<Target>();
            foreach (var client in clients)
            {
                Target target1 = new Target();
                target1.appId = APPID;
                target1.clientId = client;

                targetList.Add(target1);
            }

            String contentId = push.getContentId(message);
            String pushResult = push.pushMessageToList(contentId, targetList);
            System.Console.WriteLine("-----------------------------------------------");
            System.Console.WriteLine(pushResult);
            System.Console.WriteLine("-----------------------------------------------");
            return pushResult;
            //return string.Empty;
        }


        public static NotificationTemplate NotificationTemplateDemo(MessageBodyExtra messageBody)
        {
            NotificationTemplate template = new NotificationTemplate();
            template.AppId = APPID;
            template.AppKey = APPKEY;
            //通知栏标题
            template.Title = messageBody.title;
            //通知栏内容     
            template.Text = messageBody.msg;
            //通知栏显示本地图片
            //template.Logo = "";
            ////通知栏显示网络图标
            //template.LogoURL = "";
            //应用启动类型，1：强制应用启动  2：等待应用启动
            template.TransmissionType = 1;

            //透传内容              
            template.TransmissionContent = JsonConvert.SerializeObject(messageBody);
            //接收到消息是否响铃，true：响铃 false：不响铃   
            template.IsRing = true;
            //接收到消息是否震动，true：震动 false：不震动   
            template.IsVibrate = true;
            //接收到消息是否可清除，true：可清除 false：不可清除    
            template.IsClearable = true;
            //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
            String begin = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); // "2020-06-30 14:36:10";
            String end = DateTime.Now.AddDays(2).ToShortDateString() + " " + DateTime.Now.AddDays(2).ToLongTimeString();//"2020-06-30 22:46:20";
            template.setDuration(begin, end);

            return template;
        }

    }
    public class MessageBodyExtra
    {
        /// <summary>
        /// 类型 系统公告、其它
        /// </summary>
        public AppMessageTypeEnum type { get; set; }
        /// <summary>
        /// 跳转路径
        /// </summary>
        public string path { get; set; }
        /// <summary>
        /// 参数
        /// </summary>
        public Dictionary<string, string> param { get; set; }

        public string title { get; set; }
        public string msg { get; set; }
    }
}
